package com.ukao.pad.ui.leaveFactory;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ukao.pad.R;
import com.ukao.pad.base.BaseFragment;
import com.ukao.pad.widget.ClearEditText;
import com.ukao.pad.widget.StateImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;


public class FactoryTabFragment extends BaseFragment {


    Unbinder unbinder;
    @BindView(R.id.back_btn)
    StateImageView backBtn;
    @BindView(R.id.all_btn)
    RadioButton allBtn;
    @BindView(R.id.receive_btn)
    RadioButton receiveBtn;
    @BindView(R.id.on_passage_btn)
    RadioButton onPassageBtn;
    @BindView(R.id.shop_btn)
    RadioButton shopBtn;
    @BindView(R.id.search_edit_text)
    ClearEditText searchEditText;
    @BindView(R.id.search_button)
    StateImageView searchButton;
    @BindView(R.id.rfid_btn)
    StateImageView rfidBtn;
    @BindView(R.id.fame_layout)
    FrameLayout fameLayout;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private FactoryListFragment[] mFrament = new FactoryListFragment[4];

    public static FactoryTabFragment newInstance(String param1, String param2) {
        FactoryTabFragment fragment = new FactoryTabFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    protected void initView() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_factory_tab, container, false);
        unbinder = ButterKnife.bind(this, view);
        initFrament();
        initListener();
        return view;
    }


    private void initFrament() {
        FactoryListFragment firstFragment = findFragment(FactoryListFragment.class);
        if (firstFragment == null) {
            mFrament[0] = FactoryListFragment.newInstance("", "");
            mFrament[1] = FactoryListFragment.newInstance("1", "");
            mFrament[2] = FactoryListFragment.newInstance("2", "");
            mFrament[3] = FactoryListFragment.newInstance("3", "");


            loadMultipleRootFragment(R.id.fame_layout, 0,
                    mFrament);
            allBtn.setChecked(true);
        } else {
            mFrament[0] = firstFragment;
            mFrament[1] = findFragment(FactoryListFragment.class);
            mFrament[2] = findFragment(FactoryListFragment.class);
            mFrament[3] = findFragment(FactoryListFragment.class);

        }
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnCheckedChanged({R.id.all_btn, R.id.receive_btn, R.id.on_passage_btn, R.id.shop_btn})
    public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
        if (checked) {
            switch (buttonView.getId()) {
                case R.id.all_btn:

                    showHideFragment(mFrament[0]);

                    break;
                case R.id.receive_btn:
                    showHideFragment(mFrament[1]);
                    break;
                case R.id.on_passage_btn:
                    showHideFragment(mFrament[2]);
                    break;
                case R.id.shop_btn:
                    showHideFragment(mFrament[3]);
                    break;
            }
        }

    }


    @OnClick({R.id.back_btn, R.id.search_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.search_button:
                String strSearchEditText = getText(searchEditText);
                mFrament[radioGroup.indexOfChild(radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()))].setSearchValue(strSearchEditText);


                break;
        }
    }
}
